//
//  TimeTableDayView.m
//  iMarks
//
//  Created by Christian Speich on 06.01.08.
//  Copyright 2008 Christian Speich. All rights reserved.
//

#import "TimeTableDayView.h"
#import "UIKit/UIPreferencesTableCell.h"
#import "SubjectsManager.h"
#import "Lesson.h"

int cellSort(id cell1, id cell2, void *context);

@implementation TimeTableDayView

- (id)initWithFrame:(CGRect)frame forDay:(NSString*)day{
    self = [super initWithFrame:frame];
    if (self) {
		scroller = [[UIScroller alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
		[scroller displayScrollerIndicators];
		
		stunden = [[NSMutableArray alloc] initWithCapacity:20];

		[self makeContentForDay:day];
		[self addSubview:scroller];
    }
    return self;
}

- (void)makeContentForDay:(NSString*)day
{
	int i;
	int count;
	BOOL haveZeroLesson = NO;
	int lastLesson = 0;
	count = [[[SubjectsManager sharedInstance] mainSubjects] count]+[[[SubjectsManager sharedInstance] subjects] count];
	for (i = 0; i < count; i++) {
		Subject * obj;
		if (i >= [[[SubjectsManager sharedInstance] mainSubjects] count]) {
			obj = [[[SubjectsManager sharedInstance] subjects] objectAtIndex:i-[[[SubjectsManager sharedInstance] mainSubjects] count]];
		}
		else {
			obj = [[[SubjectsManager sharedInstance] mainSubjects] objectAtIndex:i];
		}

		int y; 
		int count2 = [[obj lessons] count];
		for (y = 0; y < count2; y++) {
			Lesson* lesson = [[obj lessons] objectAtIndex:y];
			if ([day isEqualToString:[lesson day]]) {
				UIPreferencesTableCell *hour2;
				hour2 = [[UIPreferencesTableCell alloc] initWithFrame:CGRectMake(20.f, 10.0f+50.f*([lesson lesson]), 290.0f, 50.f)];
				[hour2 setTitle:[obj name]];
				[hour2 setValue:[lesson room]];
				[hour2 setCellOutline:3];
				[stunden addObject:hour2];
				[scroller addSubview:hour2];
				
				if ([lesson lesson] == 0) {
					haveZeroLesson = YES;
				}
				if ([lesson lesson] > lastLesson) {
					lastLesson = [lesson lesson];
				}
			}
		}
	}

	[stunden sortUsingFunction:cellSort context:NULL];
	
	[[stunden objectAtIndex:0] setCellOutline:2];
	[[stunden objectAtIndex:([stunden count]-1)] setCellOutline:4];
	[scroller setContentSize:CGSizeMake([self frame].size.width, 
										([[stunden objectAtIndex:([stunden count]-1)] frame].origin.y+
										 [[stunden objectAtIndex:([stunden count]-1)] frame].size.height
										+10.0f))];
	
	float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
	float transparentWhiteComponents[4] = { 1., 1., 1., 0. };
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	
	for (i = 0; i <= lastLesson; i++) {
		NSString* label = [NSString stringWithFormat:@"%i.", i];
		UITextLabel* hour;
		hour = [[UITextLabel alloc] initWithFrame:CGRectMake(0.0f, 10.0f+50.f*i,
																 20.0f,
																 50.0f)];
		
		[hour setText:label];
		[hour setCentersHorizontally:YES];
		[hour setColor:CGColorCreate(colorSpace, white)];
		[hour setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];
		[scroller addSubview:hour];
	}
}

@end

int cellSort(id cell1, id cell2, void *context)
{
    if ([cell1 frame].origin.y < [cell2 frame].origin.y)
        return NSOrderedAscending;
    else if ([cell1 frame].origin.y > [cell2 frame].origin.y)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}
